package com.lingnan.eduservice.controller.Front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingnan.common.utils.ResultEntity;
import com.lingnan.eduservice.entity.po.EduCoursePO;
import com.lingnan.eduservice.entity.po.EduTeacherPO;
import com.lingnan.eduservice.service.EduCourseService;
import com.lingnan.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  首页功能
 *
 * @author zzd
 * @since 2021-08-31
 */
@RestController
@RequestMapping("eduservice/index")
@Api(description = "首页功能")
@CrossOrigin
public class FrontIndextController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 查询热门的课程和教师
     * @return
     */
    @GetMapping("findTeacherAndCourseByHot")
    @ApiOperation("查询热门的课程和教师")
    public ResultEntity findTeacherAndCourseByHot(){

        // 查询热门前八条课程信息
        QueryWrapper<EduCoursePO> courseWrapper = new QueryWrapper<>();

        // 根据查看记录作为条件
        courseWrapper.orderByDesc("view_count");

        // 查询前八条
        courseWrapper.last(" LIMIT 8");

        // 执行查询
        List<EduCoursePO> courseList = eduCourseService.list(courseWrapper);

        // 查询热门前四名教师
        QueryWrapper<EduTeacherPO> teacherWrapper = new QueryWrapper<>();

        // 根据资历和时间进行排序
        teacherWrapper.orderByAsc("gmt_modified");

        // 查询前四条记录
        teacherWrapper.last(" LIMIT 4");

        // 执行查询
        List<EduTeacherPO> teacherList = eduTeacherService.list(teacherWrapper);

        // 返回查询信息
        return ResultEntity.ok().data("courseList",courseList).data("teacherList",teacherList);

    }
}
